package com.demo.hive;

//import org.apache.hive.jdbc.HiveDataSource;

//import javax.sql.DataSource;
import java.sql.*;

public class MyHiveTemplate {
    static Connection hiveConn;
    static {
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");//需要打开hive-jdbc.1.1.0-cdh5.16.2这个依赖

            hiveConn = DriverManager.getConnection("jdbc:hive2://hadoop-1:10000/default", "hive", "");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        selectObject();

        closeConn();
    }

    private static void closeConn() {
        try {
            hiveConn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void selectObject() {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = hiveConn.createStatement();
            boolean execute = false;
            execute = statement.execute("SELECT * FROM `default`.`date_table` LIMIT 1000");
            if(execute){
                resultSet = statement.getResultSet();
                while (resultSet.next()){
                    String string = resultSet.getString(1);
                    System.out.println(string);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
}
